package aeolus.wind.spider.web.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import aeolus.wind.spider.biz.dataobject.Ability;
import aeolus.wind.spider.biz.dataobject.Identity;
import aeolus.wind.spider.biz.dataobject.Vote;
import aeolus.wind.spider.web.commons.BaseController;
import aeolus.wind.spider.web.commons.Constants;
import aeolus.wind.spider.web.commons.vote.VoteView;

@Controller
public class VoteController extends BaseController {

    @RequestMapping(value = "saveVote.htm")
    public ModelAndView saveVote(String votedName, String votedRes, ModelMap model, boolean isAsUser) {
        String voteName = getLoginName();// 投票者
        String voteOwnerName = null;// 本次投票的所属负责人
        if (isAsUser) {
            voteOwnerName = getLeaderName();
            // 得到领导者所在团队的相关投票
        } else if (checkCanLeadGroup()) {
            voteOwnerName = getLoginName();
        }
        if (StringUtils.isNotBlank(voteOwnerName) && StringUtils.isNotBlank(votedRes) && checkLogin()) {
            String[] abilityNameWithValueArray = votedRes.split(";;");
            for (String abilityNameWithValue : abilityNameWithValueArray) {
                String[] tmpArray = abilityNameWithValue.split(",,");
                String abName = tmpArray[0];
                int value = Integer.valueOf(tmpArray[1]);
                if (value <= Constants.MAX_VALUE && value > Constants.MIN_VALUE) {
                    Vote vote = new Vote();
                    vote.setIdentityNameVote(voteName);
                    vote.setIdentityNameBeVoted(votedName);
                    vote.setAbilityName(abName);
                    vote.setOwnIdentityName(voteOwnerName);
                    vote.setValue(value);
                    voteService.saveVote(vote);
                }
            }

        }
        String lastVotedEncode = getEncodedString(votedName);
        return new ModelAndView(new RedirectView("manageVote.htm?isAsUser=" + isAsUser + "&lastVoted=" + lastVotedEncode
                                                 + "#" + lastVotedEncode));
    }

    @RequestMapping(value = "manageVote.htm")
    public String manageVote(String lastVoted, ModelMap model, boolean isAsUser) {
        if (checkLogin()) {
            String voteName = getLoginName();// 投票者
            String voteOwnerName = null;// 本次投票的所属负责人
            if (isAsUser) {
                voteOwnerName = getLeaderName();
                // 得到领导者所在团队的相关投票
            } else if (checkCanLeadGroup()) {
                voteOwnerName = getLoginName();
            }
            if (voteOwnerName != null) {
                // 得到自己领导的团队的相关投票
                List<Ability> abilityList = abilityService.listAbilities(voteOwnerName);// 所有能力的列表
                Map<String, Vote> voteMap = voteService.getVoteMadeBy(voteOwnerName, voteName);// 已经投票过的
                List<Identity> identities = identityService.listIdentitiesLeaded(voteOwnerName);// 所有需要投票的成员的列表
                model.addAttribute("voteView", new VoteView(abilityList, identities, voteMap).getVoteByName());
                // 做页面自动调整
                if (lastVoted != null) {
                    // lastVoted = getUtfString(lastVoted);
                    model.addAttribute("lastVoted", lastVoted);
                }
            }
        }
        model.put("isAsUser", isAsUser);
        return getScreen("manageVote");
    }

}
